package com.tdrc.common.util;

import org.springframework.stereotype.Component;

import java.io.File;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class CommonUtil {
	/**
	 * 根据文件后缀名生成新的上传文件名
	 * 
	 * @param ext
	 * @return
	 */
	public static String getUploadFileName(String ext) {
		StringBuffer sb = new StringBuffer();
		int r = (int) (Math.random() * (90000 - 10000 + 1) + 10000);
		LocalDateTime date = LocalDateTime.now();
		sb.append(date.getYear());
		sb.append(date.getMonthValue() < 10 ? "0" + date.getMonthValue() : date.getMonthValue());
		sb.append(date.getDayOfMonth() < 10 ? "0" + date.getDayOfMonth() : date.getDayOfMonth());
		sb.append(date.getHour() < 10 ? "0" + date.getHour() : date.getHour());
		sb.append(date.getMinute() < 10 ? "0" + date.getMinute() : date.getMinute());
		sb.append(date.getSecond() < 10 ? "0" + date.getSecond() : date.getSecond());
		sb.append(r);
		sb.append(ext);
		return sb.toString();
	}

	/**
	  * 删除单个文件 
	 * filePath为文件存储真实
	 * 
	 * @param filePath
	 */
	public void delSingleFile(String filePath) {
		File file = new File(filePath);
		if (file.isFile() && file.exists()) {
			file.delete();
		}
	}

	/**
	 * 获取文件后缀名
	 * 
	 * @param fileName
	 * @return
	 */
	public static String getSuffix(String fileName) {
		if (fileName != null && !fileName.equals("")) {
			int pos = fileName.lastIndexOf(".");
			return fileName.substring(pos);
		} else {
			return null;
		}
	}
	/**
     * 获取apk的大小【单位：KB】
     *
     * @param file 文件
     * @return 文件长度
     */
    public static int getApkFileSize(final File file) {
        long fileLength = getFileLength(file);
        return (int) (fileLength / 1024);
    }
    /**
     * 获取文件长度
     *
     * @param file 文件
     * @return 文件长度
     */
    private static long getFileLength(final File file) {
        if (!isFile(file)) {
			return -1;
		}
        return file.length();
    }
    /**
     * 判断是否是文件
     *
     * @param file 文件
     * @return {@code true}: 是<br>{@code false}: 否
     */
    private static boolean isFile(final File file) {
        return file != null && file.exists() && file.isFile();
    }
	/**
	 * yyyy-MM-dd HH:mm:ss 字符串转LocalDateTime
	 * 
	 * @param dateStr
	 * @return
	 */
	public static LocalDateTime strToDateTime(String dateStr) {
		DateTimeFormatter dtt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

		return LocalDateTime.parse(dateStr, dtt);
	}
	  /**
     * 转换文件大小
     */
    public static String formetFileSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileS == 0) {
            return wrongSize;
        }
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
        }
        return fileSizeString;
    }
	/**
	 * yyyy-MM-dd HH:mm:ss LocalDateTime转字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String dateTimeToStr(LocalDateTime date) {
		DateTimeFormatter dtt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		return dtt.format(date);

	}

	/**
	 * yyyy-MM-dd HH:mm:ss:SSS LocalDateTime转字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String dateTimeMillToStr(LocalDateTime date) {
		DateTimeFormatter dtt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
		return dtt.format(date);

	}

	/**
	 * yyyy-MM-dd HH:mm:ss:SSS 字符串转LocalDateTime
	 * 
	 * @param dateStr
	 * @return
	 */
	public static LocalDateTime strToDateTimeMill(String dateStr) {
		DateTimeFormatter dtt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
		return LocalDateTime.parse(dateStr, dtt);
	}

	/**
	 * yyyy-MM-dd 字符串转LocalDate
	 * 
	 * @param dateStr
	 * @return
	 */
	public static LocalDate strToDate(String dateStr) {
		DateTimeFormatter dtt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		return LocalDate.parse(dateStr, dtt);
	}

	/**
	 * yyyy-MM-dd LocalDate 转字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToStr(LocalDate date) {
		DateTimeFormatter dtt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		return date.format(dtt);
	}
	/**
	 * 获取当前时间的时间戳
	 * @param date
	 * @return
	 */
	 public static String getTimeStamp(LocalDateTime date) {
		   DateTimeFormatter dtt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
		   return dtt.format(date);
	   }

	/**
	 * 获取当前时间字符串，时间格式为yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getNowDateTime(){
	 	return dateTimeToStr(LocalDateTime.now());
	 }
	/**
	 * 判断对象是否为null
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public boolean isAllFieldNull(Object obj) throws Exception {
		// 得到类对象
		Class<?> stuCla = (Class<?>) obj.getClass();
		// 得到属性集合
		Field[] fs = stuCla.getDeclaredFields();
		boolean flag = true;
		for (Field f : fs) {
			f.setAccessible(true);
			Object val = f.get(obj);
			if (val != null) {
				flag = false;
				break;
			}
		}
		return flag;
	}

	/**
	 * 判断字符串是否为整数
	 * 
	 * @param str
	 * @return
	 */
	public boolean isNumber(String str) {
		Pattern pattern = Pattern.compile("-?[0-9]*");
		Matcher isNum = pattern.matcher(str);
		return isNum.matches();
	}

	/**
	 * 判断数组是否有重复元素
	 * 
	 * @param array
	 * @return
	 */
	public boolean cheakArrayRepeat(Object[] array) {
		HashSet<Object> hashSet = new HashSet<Object>();
		for (int i = 0; i < array.length; i++) {
			hashSet.add(array[i]);
		}
		if (hashSet.size() == array.length) {
			return true;
		} else {
			return false;
		}
	}
}
